package com.qiangqiang.adapter;

import com.qiangqiang.dbpool.ConPool;
import com.qiangqiang.dbpool.MyCon;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by q on 2018/4/6.
 */
public class SqlAdapter extends SqlAdaptee implements SqlHandler{

    private PreparedStatement pstm;
    private ResultSet resultSet;


    public void execute(String sql) {
        try {
            System.out.println(sql);
            MyCon myCon = ConPool.getInstance().getCon();
            pstm = myCon.getCon().prepareStatement(sql);
            pstm.execute();
            pstm.close();
            ConPool.getInstance().setFree(myCon);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Object executeQuery(String sql, Class<?> c) {
        MyCon myCon = ConPool.getInstance().getCon();
        try {
            pstm = myCon.getCon().prepareStatement(sql);
            resultSet = pstm.executeQuery();
            return setObjectBean(resultSet,c);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
